package controller
{
	import com.adobe.serialization.json.*;
	
	import models.CoLUM;
	import models.EF;
	import models.FD;
	import models.NFA;
	import models.IE;
	
	import mx.collections.ArrayCollection;
	import mx.collections.ArrayList;
	import mx.controls.Alert;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;
	
	public class DataFactory
	{
		public var dataCollection:ArrayCollection;
		private var dataId:String;
		private var countryId:String;
		public var countryCollection:ArrayCollection;
		
		public function DataFactory(dataIdentifier:String, countryIdentifier:String)
		{
			countryId = countryIdentifier;
			dataId = dataIdentifier;
			dataCollection = new ArrayCollection();
			
		}
		
		private var service:HTTPService;
		
		public function getCollectionFactory(url:String):void
		{		

				service = new HTTPService();
				service.url = url;
				service.method = "GET";
				service.addEventListener("result", httpResult);
				service.addEventListener("fault", httpFault);
				service.send();
		}
		
		public function httpResult(event:ResultEvent):void {
			
			var result:Object = event.result;
			var object:Object = JSON.decode(result.toString());

			var array:Array = object as Array;
			
			var objectify:Object;
			
			if (dataId == "colum"){
				while (objectify = array.pop()){
					var year:int = objectify.year;
					var land_type:String = objectify.land_type;
					var category:String = objectify.category;
					var item:String = objectify.item;
					var amount:Number = objectify.amount;
					var CoLUM_single:CoLUM = new CoLUM(countryId, year, land_type, category, item, amount);
					
					dataCollection.addItem(CoLUM_single);
				}
			}
			
			else if (dataId == "ef"){
				while (objectify = array.pop()){
					var year:int = objectify.year;
					var land_type:String = objectify.land_type;
					var ef_type:String = objectify.EF_type;
					var name:String = objectify.name;
					var amount:Number = objectify.amount;
					var EF_single:EF = new EF(countryId, year, land_type, ef_type, name, amount);
					
					dataCollection.addItem(EF_single);
				}
			}
			
			else if (dataId == "fd"){
				while (objectify = array.pop()){
					var year:int = objectify.year;
					var category:String = objectify.category;
					var item:String = objectify.item;
					var amount:Number = objectify.amount;
					var FD_single:FD = new FD(countryId, year, category, item, amount);
					
					dataCollection.addItem(FD_single);
				}
			}
			
			else if (dataId == "nfa"){
				while (objectify = array.pop()){
					var year:int = objectify.year;
					var per_capita:int = objectify.per_capita;
					var land_type:String = objectify.land_type;
					var ef_type:String = objectify.EF_type;
					var amount:Number = objectify.amount;
					var NFA_single:NFA = new NFA(countryId, year, per_capita, land_type, ef_type, amount);
					
					dataCollection.addItem(NFA_single);
				}
			}
			
			else if (dataId == "ie"){
				while (objectify = array.pop()){
					var year:int = objectify.year;
					var country_id:String = objectify.country_id;
					var land_type:String = objectify.land_type;
					var trade_type:String = objectify.trade_type;
					var amount:Number = objectify.amount;
					//var country_name:String = getCountryName(country_id);
					var IE_single:IE = new IE(countryId, year, country_id, land_type, trade_type, amount);
					
					dataCollection.addItem(IE_single);
				}
			}

		}
		
		public function httpFault(event:FaultEvent):void {
			var faultstring:String = event.fault.faultString;
			Alert.show(faultstring);
		}
		
		public function getCollection():ArrayCollection 
		{
			return dataCollection;
		}
		
		/*public function getCountryName(country_id:String){
			for ()
		}*/
		
	}
}